package ext.acae2.change.helper;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Vector;

import com.ptc.windchill.enterprise.team.server.TeamCCHelper;

import wt.change2.ChangeActivityIfc;
import wt.change2.ChangeException2;
import wt.change2.ChangeHelper2;
import wt.change2.WTChangeActivity2;
import wt.change2.WTChangeOrder2;
import wt.fc.ObjectReference;
import wt.fc.PersistenceHelper;
import wt.fc.QueryResult;
import wt.fc.WTObject;
import wt.lifecycle.LifeCycleManaged;
import wt.maturity.MaturityHelper;
import wt.method.RemoteMethodServer;
import wt.org.WTPrincipal;
import wt.org.WTPrincipalReference;
import wt.project.Role;
import wt.team.Team;
import wt.team.TeamHelper;
import wt.team.TeamReference;
import wt.util.WTException;
import ext.acae2.change.utility.ChangeUtility;


/**
 * @desription 
 *    ECN change helper
 * @author cnie
 * @date 2013-5-11 下午4:45:36
 */
public class ECNHelper {
	
	
	/**
	 * @description 
	 *    get affect object by ecn
	 * @author cnie
	 * @date 2013-5-11 下午5:26:08
	 * @version 1.0
	 * @param ecn
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public static ArrayList<WTObject> getChangeAffectItem(WTChangeOrder2 ecn){
		try{
			if(!RemoteMethodServer.ServerFlag){
				return (ArrayList<WTObject>) RemoteMethodServer.getDefault().invoke("getChangeAffectItemRemote", ECNHelper.class.getName(), null,
						new Class[] {WTChangeOrder2.class},
						new Object[] {ecn});
			}else{
				return getChangeAffectItemRemote(ecn);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	private static ArrayList<WTObject> getChangeAffectItemRemote(WTChangeOrder2 ecn){
		List<WTChangeActivity2> cAs = ChangeUtility.getReleatedCA(ecn);
		ArrayList<WTObject> resultItems = new ArrayList<WTObject>();
		for (int i = 0; i < cAs.size(); i++) {
			WTChangeActivity2 ca = cAs.get(i);
			resultItems = ChangeUtility.getCAAffectedData(ca);
		}
		return resultItems;
	}
	
	
	public static WTChangeOrder2 getChangeOrderByChangeActivity(ChangeActivityIfc ca){
		QueryResult qr = new QueryResult();
		try {
			qr = ChangeHelper2.service.getChangeOrder(ca);
			if(qr.hasMoreElements()){
				return (WTChangeOrder2)qr.nextElement();
			}
		} catch (ChangeException2 e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (WTException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	public static ArrayList<WTObject> getChangeResultingItem(WTChangeOrder2 ecn){
		try{
			if(!RemoteMethodServer.ServerFlag){
				return (ArrayList<WTObject>) RemoteMethodServer.getDefault().invoke("getChangeResultingItem", ECNHelper.class.getName(), null,
						new Class[] {WTChangeOrder2.class},
						new Object[] {ecn});
			}else{
				return getChangeResultingItemRemote(ecn);
			}
		}catch(Exception e){
			e.printStackTrace();
		}
		return null;
	}
	
	private static ArrayList<WTObject> getChangeResultingItemRemote(WTChangeOrder2 ecn){
		List<WTChangeActivity2> cAs = ChangeUtility.getReleatedCA(ecn);
		ArrayList<WTObject> resultItems = new ArrayList<WTObject>();
		for (int i = 0; i < cAs.size(); i++) {
			WTChangeActivity2 ca = cAs.get(i);
			resultItems = ChangeUtility.getCAResultingData(ca);
		}
		return resultItems;
	}
	
	
	public static void addPrincipalsFromECN(ObjectReference self,Object obj){
		if(obj instanceof WTChangeOrder2){
			WTChangeOrder2 ecn  = (WTChangeOrder2)obj;
			try{
				Team ecnTeam = (Team) TeamCCHelper.getTeamFromObject(ecn);
				if(ecnTeam == null)
					return ;
				ecnTeam  = (Team) PersistenceHelper.manager.refresh(ecnTeam);
				Vector ecnRole = TeamHelper.service.findRoles(ecnTeam);
				HashMap ecnRoleMap = TeamHelper.service.findAllParticipantsByRole(ecnTeam);
				ArrayList<WTObject> resultItems = ECNHelper.getChangeResultingItem(ecn);
				//MaturityHelper.getService().getPromotionTargets(pn);
				for(WTObject wtobject:resultItems){
					LifeCycleManaged lcm = (LifeCycleManaged)wtobject;
					Team objTeam = (Team) TeamCCHelper.getTeamFromObject(lcm);
					if(objTeam==null)return;
					objTeam  = (Team) PersistenceHelper.manager.refresh(objTeam);
					for(Object role:ecnRole){
						ArrayList principalList = (ArrayList) ecnRoleMap.get(role);
						for(java.util.Iterator it=principalList.iterator();it.hasNext();){
							WTPrincipal principal = ((WTPrincipalReference)it.next()).getPrincipal();
							objTeam.addPrincipal((Role) role, principal);
						}
					}
					TeamReference teamReference = TeamReference.newTeamReference(objTeam);
					TeamHelper.service.augmentRoles(lcm, teamReference);
				}
				
			}catch(Exception e){
				e.printStackTrace();
			}
		}
	}
	
	
	
	
	
	
}
